#include <stdio.h>
#include <stdlib.h>

// m 是迷宫矩阵 r是行数 c是列数 i是当前步数
void maze(int **m, int r, int c, int ox, int oy, int x, int y, int i)
{
    if (x == ox && y == oy)
    {
        m[x][y] = i;
        for (int n = 0; n < r; n++)
        {
            for (int l = 0; l < c; l++)
                printf("%3d", m[n][l]);
            printf("\n");
        }
        printf("\n");
        m[x][y] = 0;
        return;
    }
    if (x < 0 || x >= r || y < 0 || y >= c)
        return;
    if (m[x][y] != 0)
        return;
    m[x][y] = i;
    maze(m, r, c, ox, oy, x + 1, y, i + 1);
    maze(m, r, c, ox, oy, x - 1, y, i + 1);
    maze(m, r, c, ox, oy, x, y + 1, i + 1);
    maze(m, r, c, ox, oy, x, y - 1, i + 1);
    m[x][y] = 0;
}

int main()
{
    /*******************code*******************/
    int m[][6] = {{0, -1, 0, 0, 0, 0},
                  {0, -1, 0, -1, 0, 0},
                  {0, -1, 0, -1, -1, -1},
                  {0, 0, 0, 0, 0, 0},
                  {0, -1, -1, 0, -1, 0},
                  {0, 0, -1, 0, 0, 0}};
    int **p = (int **)malloc(sizeof(int *) * 6);
    for (int i = 0; i < 6; i++)
        p[i] = m[i];
    int ox = 5, oy = 5, x = 0, y = 0;
    maze(p, 6, 6, ox, oy, x, y, 1);
    /******************************************/
    fflush(stdin);

    printf("\n\n****************************\n");
    printf("Press Enter key to continue\n");
    getchar();
    return 0;
    /******************************************/
}
